﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace 视频设备
{
    public partial  class 生成验证码
    {
        public enum YanZhengResult
        {
            Success,Fail,CodeNull
        }
        static object[] CreateRegionCode(int strLength)
        {
            var rBase = new[]
            {
                "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f" 
            };
            var random = new Random();
            var bytes = new object[strLength];
            /**/
            /*每循环一次产生一个含两个元素的十六进制字节数组，并将其放入bject数组中 
每个汉字有四个区位码组成 
区位码第1位和区位码第2位作为字节数组第一个元素 
区位码第3位和区位码第4位作为字节数组第二个元素 
*/
            for (int i = 0; i < strLength; i++)
            {
                //区位码第一位
                var r1 = random.Next(11, 14);
                var str_r1 = rBase[r1].Trim();
                random = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值 
                var r2 = 0;
                if (r1 == 13)
                    r2 = random.Next(0, 7);
                else
                    r2 = random.Next(0, 16);
                var str_r2 = rBase[r2].Trim();
                //区位码第三位
                random = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
                var r3 = random.Next(10, 16);
                var str_r3 = rBase[r3].Trim();
                //区位码第四位
                random = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
                var r4 = 0;
                if (r3 == 10)
                    r4 = random.Next(1, 16);
                else if (r3 == 15)
                    r4 = random.Next(0, 15);
                else
                    r4 = random.Next(0, 16);
                var str_r4 = rBase[r4].Trim();
                var byte1 = Convert.ToByte(str_r1 + str_r2, 16);
                var byte2 = Convert.ToByte(str_r3 + str_r4, 16);
                //将两个字节变量存储在字节数组中 
                var str_r = new[] { byte1, byte2 };
                //将产生的一个汉字的字节数组放入object数组中 
                bytes.SetValue(str_r, i);
            }
            return bytes;
        }
        
        //生成数据验证码 
        public  string GenerateCheckCode()
        {
            int number;
            char code;
            string checkCode = String.Empty;
            var random = new Random();
            for (int i = 0; i < 5; i++)
            {
                number = random.Next();
                if (number % 2 == 0)
                    code = (char)('0' + (char)(number % 10));
                else
                    code = (char)('A' + (char)(number % 26));
                checkCode += code.ToString();
            }
            System.Web.HttpContext.Current.Session["CheckCode"] = checkCode;
            return checkCode;
        }


        public static  YanZhengResult  CheckCodeSame(string checkCode)
        {           
         var abc=  System.Web.HttpContext.Current.Session["CheckCode"].ToString().ToUpper() ;
         if (HttpContext.Current.Session["CheckCode"] != null)
         {
             if (checkCode.ToUpper() == abc)
             {
                 return YanZhengResult.Success;
             }
             else
             {
                 return YanZhengResult .Fail ;
             }
         }
         else
         {
             return YanZhengResult.CodeNull;
         }
        }
        
    }
}